package com.itboye.textileexpres.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.DetailBean;
import com.itboye.textileexpes.beans.YoufeiBean;
import com.itboye.textileexpes.presenter.YouFeiPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.myset.ActivityAddressManage;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;
/**
 * 
 * @author 付款
 *
 */
public class ActivityOrderOk extends BaseActivity implements Observer {
	TextView title;
	ImageView back;
	ImageView img_other;
	TextView choose_tv;
	LinearLayout relcontain;
	LinearLayout address_lin;
	LinearLayout choose_address_lin;
	TextView shouhuo_name, textPhone, address_tv, faburen, tvcandi;
	ImageView goods_img;
	TextView num_tv, total_price, tvprice;
	EditText liuyan;
	String addressID;
	String name;
	String detailInfo;
	String phone;
	String pids;
	String goodPrice;
	StringBuffer count;
	TextView queren_tv;
	TextView heji_price;
	YouFeiPresenter youFeiPresenter;
	List<YoufeiBean> youfeibean = new ArrayList<>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_detail_order;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText("确认订单");
		
	}

	public void onOrderOk(){
		youFeiPresenter = new YouFeiPresenter(this);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		choose_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityOrderOk.this,
						ActivityAddressManage.class);
				intent.putExtra("type", "goods_detail");
				startActivityForResult(intent, 101);
			}
		});
		queren_tv.setEnabled(false);
		DetailBean detailBean = (DetailBean) getIntent().getSerializableExtra(
				"detail");
		for (int i = 0; i < detailBean.getCode().size(); i++) {
			View view = LayoutInflater.from(ActivityOrderOk.this).inflate(
					R.layout.item_detail_queren, null);
			TextView goods_name1_tv = (TextView) view
					.findViewById(R.id.goods_name1_tv);
			TextView goods_name2_tv = (TextView) view
					.findViewById(R.id.goods_name2_tv);
			TextView goods_detail_tv = (TextView) view
					.findViewById(R.id.goods_detail_tv);
			TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView price_unit_tv = (TextView) view
					.findViewById(R.id.price_unit_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
			ImageView goods_img = (ImageView) view.findViewById(R.id.goods_img);
			goods_name1_tv.setText("" + detailBean.getCode().get(i));
			goods_name2_tv.setText(detailBean.getGoodsname());
			goods_detail_tv.setText(detailBean.getSecondline());
			color_tv.setText(detailBean.getDesc().get(i));
			price_tv.setText(detailBean.getSingle_price().get(i));
			num_tv.setText(detailBean.getCount().get(i));
			count = new StringBuffer();
			count.append("," + detailBean.getCount().get(i));
			XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
					+ detailBean.getImg().get(i), goods_img);
			relcontain.addView(view);
		}
		pids = detailBean.getPskid();
		faburen.setText(detailBean.getName());
		tvcandi.setText(detailBean.getChandi());
		total_price.setText(detailBean.getTotal());
		goodPrice = (detailBean.getTotal());

		queren_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityOrderOk.this,
						ActivityPaymentMethod.class);
				intent.putExtra("price", heji_price.getText() + "");
				startActivity(intent);
			}
		});
	}
	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == YouFeiPresenter.youfei_success) {
				youfeibean.addAll((List<YoufeiBean>) entity.getData());
				tvprice.setText(youfeibean.get(0).getFreight());
				heji_price.setText(""
						+ (Double.parseDouble(goodPrice) + Double
								.parseDouble(youfeibean.get(0).getFreight())));
			}
		}

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 102) {
			if (arg2 != null) {
				address_lin.setVisibility(View.VISIBLE);
				choose_address_lin.setVisibility(View.GONE);
				addressID = arg2.getStringExtra("id");
				name = arg2.getStringExtra("name");
				phone = arg2.getStringExtra("phone");
				detailInfo = arg2.getStringExtra("address");
				shouhuo_name.setText(name);
				textPhone.setText(phone);
				address_tv.setText(detailInfo);
				queren_tv.setEnabled(true);
				youFeiPresenter.getYouFei(IsMobileNumPhone.isUid(),
						count.substring(1, count.length()), pids, "277");

			}

		}
	}

}
